package cn.zhangjie.order.service.impl;

import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.order.domain.OrderPetAcquisition;
import cn.zhangjie.order.dto.OrderDto;
import cn.zhangjie.order.mapper.OrderPetAcquisitionMapper;
import cn.zhangjie.order.query.OrderPetAcquisitionQuery;
import cn.zhangjie.order.service.IOrderPetAcquisitionService;

import cn.zhangjie.org.domain.Employee;
import cn.zhangjie.org.domain.Shop;
import cn.zhangjie.org.mapper.EmployeeMapper;
import cn.zhangjie.org.mapper.ShopMapper;
import cn.zhangjie.pet.domain.Pet;
import cn.zhangjie.pet.mapper.PetMapper;
import cn.zhangjie.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * (OrderPetAcquisition)表服务实现类
 *
 * @author makejava
 * @since 2022-08-11 11:06:16
 */
@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class OrderPetAcquisitionServiceImpl extends BasicServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService  {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, Long id) {
        /**
         * shopId
         * empId
         */
        Employee employee = employeeMapper.loadByLogininId(id);
        if(employee.getShop_id()!=null){

            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdmin_id()!=employee.getId()){
                query.setEmpId(employee.getId());
                System.out.println(employee.getId());
            }
            // 店铺人员
            query.setShopId(employee.getShop_id());
            System.out.println(employee.getShop_id());
        }

        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto dto) {
        Long ordreId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();
        /**
         * 1. 改订单状态，金额, 支付方式
         * 2. 改寻主消息状态
         * 3. 改pet金额
         */
        // 1.改订单状态，金额, 支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(ordreId);
        orderPetAcquisition.setState(2);//成功
        orderPetAcquisition.setPrice(money);//钱
        orderPetAcquisition.setPaytype(payType);//支付方式
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 2.改寻主消息状态
        searchMasterMsgMapper.updateState(orderPetAcquisition.getSearch_master_msg_id());

        // 3.改pet金额
        Pet pet = petMapper.loadById(orderPetAcquisition.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);

    }

    @Override
    public void cancel(Long id) {
        /**
         * 1. 修改订单状态
         * 2. 寻主消息的shopid=null
         * 3. pet 删除
         */
        // 1. 修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        System.out.println(orderPetAcquisition);
        orderPetAcquisition.setState(-1); // 取消
        orderPetAcquisitionMapper.update(orderPetAcquisition);


        // 2. 寻主消息的shopid=null
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearch_master_msg_id());

        // 3. pet 删除
        petMapper.remove(orderPetAcquisition.getPet_id());
    }
}
